<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\HospitalAppointType;
use App\Models\SystemNotice;
use App\Models\TempPushRecord;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;

class AppointNumJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $hospitalAppointId;
    protected $day;
    public $tries = 0;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($hospitalAppointId,$day=false)
    {
        $this->hospitalAppointId = $hospitalAppointId;
        $this->day = $day;
        $this->onQueue('appoint-num');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        try {
            AppointNum::SetupScheduling($this->hospitalAppointId,$this->day);
        } catch (\Exception $exception) {
            $this->fail($exception->getMessage());
        }

    }

//    /**
//     * 获取作业的唯一 ID。
//     */
//    public function uniqueId(): string
//    {
//        return $this->appoint->hospital_id."-".$this->appoint->type;
//    }

    /**
     * 同一条数据同一时间只处理一次。
     *
     * @return array<int, object>
     */
    public function middleware(): array
    {
        return [(new WithoutOverlapping($this->hospitalAppointId))->dontRelease()];
    }
}
